﻿<form id="frmQuery" action="/query/validate" method="post" class="form-horizontal" autocomplete="off">
<fieldset>@Html.Hidden("hsmsInterval", ViewData["smsInterval"])
    <div class="control-group" style="margin-top: 32px;">
        <label for="txtIdCard" class="control-label">
            身份证号：</label>
        <div class="controls">
            <input type="text" id="txtIdCard" name="txtIdCard" class="inputText" style="width: 210px;" />
        </div>
        <div class="errorPlace" id="idCardErrorPanel">
        </div>
    </div>
    <div class="control-group">
        <label for="txtPhone" class="control-label">
            手机号：</label>
        <div class="controls">
            <input type="text" name="txtPhone" id="txtPhone" class="required" style="width: 132px;"
                value="" />
            <input type="button" value="获取验证码" id="btnSend" onclick="SendVCode()" />
        </div>
        <div class="errorPlace" id="phoneErrorPanel">
        </div>
    </div>
    <div class="control-group">
        <label for="txtVCode" class="control-label">
            验证码：</label>
        <div class="controls">
            <input type="text" name="txtVCode" id="txtVCode" class="span2" style="width: 210px;"
                value="" />
        </div>
        <div class="errorPlace">
        </div>
    </div>
    <div class="control-group">
        <label for="" class="control-label" style="margin-left:45px;_margin-left:35px;font-size:11px;*width:168px;">
        <input type="checkbox" id="chkAgree" style="border: 0 none;display: inline;width: 20px;" /> 
        <span style="font-weight:normal;color:#777;">同意遵守</span> 
        <a href="javascript:void(0);" onclick="return showProtocol();" style="color: #027AC8; text-decoration: underline;"><b>用户协议</b></a>
        </label>
        <div class="controls" style="*width:100px;*padding-left:20px;">
        <input value="查 询" id="btnSearch" type="image" src="/images/search.jpg" style="margin-left: 75px;*margin-left: 35px;_margin-left: 5px;display:inline;width:100px;height:30px;border:0;" />
        </div>
    </div>
</fieldset>
</form>
<script type="text/javascript">
    $(document).ready(function () {
        jQuery.validator.addMethod("isIdCardNo", function (value, element) {
            return this.optional(element) || isIdCardNo(value);
        }, "请正确输入您的身份证号码");
        //手机号码验证      
        jQuery.validator.addMethod("isMobile", function (value, element) {
            var length = value.length;
            var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "请正确填写您的手机号码");
        jQuery.validator.addMethod("IdCardExist", function (value, element) {
            var result = false;
            // 设置同步
            $.ajaxSetup({
                async: false
            });
            var param = {
                idCard: value
            };
            $.post("/interface/isidcardexist", param, function (data) {
                result = (1 == data);
            });
            // 恢复异步
            $.ajaxSetup({
                async: true
            });
            return result;
        });
        jQuery.validator.addMethod("PhoneExist", function (value, element) {
            var result = false;
            // 设置同步
            $.ajaxSetup({
                async: false
            });
            var param = {
                phone: value
            };
            $.post("/interface/isphoneexist", param, function (data) {
                result = ("1" == data);
            });
            // 恢复异步
            $.ajaxSetup({
                async: true
            }); //alert(result);
            return result;
        }, "系统不存在此手机号的贷款记录");
    });

    //增加身份证验证
    function isIdCardNo(num) {
        var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
        var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
        var varArray = new Array();
        var intValue;
        var lngProduct = 0;
        var intCheckDigit;
        var intStrLen = num.length;
        var idNumber = num;
        // initialize
        if ((intStrLen != 15) && (intStrLen != 18)) {
            return false;
        }
        // check and set value
        for (i = 0; i < intStrLen; i++) {
            varArray[i] = idNumber.charAt(i);
            if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
                return false;
            } else if (i < 17) {
                varArray[i] = varArray[i] * factorArr[i];
            }
        }

        if (intStrLen == 18) {
            //check date
            var date8 = idNumber.substring(6, 14);
            if (isDate8(date8) == false) {
                return false;
            }
            // calculate the sum of the products
            for (i = 0; i < 17; i++) {
                lngProduct = lngProduct + varArray[i];
            }
            // calculate the check digit
            intCheckDigit = parityBit[lngProduct % 11];
            // check last digit
            if (varArray[17] != intCheckDigit) {
                return false;
            }
        }
        else {        //length is 15
            //check date
            var date6 = idNumber.substring(6, 12);
            if (isDate6(date6) == false) {
                return false;
            }
        }
        return true;
    }
    function isDate6(sDate) {
        if (!/^[0-9]{6}$/.test(sDate)) {
            return false;
        }
        var year, month, day;
        year = sDate.substring(0, 4);
        month = sDate.substring(4, 6);
        if (year < 1700 || year > 2500) return false
        if (month < 1 || month > 12) return false
        return true
    }
    /**
    * 判断是否为“YYYYMMDD”式的时期
    *
    */
    function isDate8(sDate) {
        if (!/^[0-9]{8}$/.test(sDate)) {
            return false;
        }
        var year, month, day;
        year = sDate.substring(0, 4);
        month = sDate.substring(4, 6);
        day = sDate.substring(6, 8);
        var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if (year < 1700 || year > 2500) return false
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
        if (month < 1 || month > 12) return false
        if (day < 1 || day > iaMonthDays[month - 1]) return false
        return true
    }
    $(function () {
//        $("#frmQuery").validate();

//        $("#txtIdCard").blur(function () {
//            IdCardExist();
//        })
//        $("#txtPhone").blur(function () {
//            //PhoneExist();
//        })
    });

//    function IdCardExist() {
//       return $.get("/interface/isidcardexist", { idcard: $("#txtIdCard").val() }, function (data) {
//           if (data == 0) {
//               $("#idCardErrorPanel").html('<label for="txtIdCard" generated="true" class="error">不存在此身份证号的贷款记录33</label>')
//                return false;
//            }
//            return true;
//        });
//    }

//    function PhoneExist() {
//        return $.get("/interface/isphoneexist", { phone: $("#txtPhone").val() }, function (data) {
//            alert(data);
//            if (data == 0) {
//                $("#phoneErrorPanel").html('<label for="txtPhone" generated="true" class="error">系统不存在此手机号的贷款记录33</label>')
//                return false;
//            }
//            return true;
//        });
//    }

    function VValicate() {
        $.get("/query/VCodeValidate", { vcode: $("#txtVCode").val() }, function (data) {
            if (data == 0) {
                alert("验证码输入有误或者已过有效期，请重新获取。");
                return false;
            }
            else {
                return true;
            }
        })
    }
    function Fit4GetVCode() {
        if ($("#txtIdCard").val() == "") {
            $("#idCardErrorPanel").html('<label for="txtIdCard" generated="true" class="error">请输入身份证号</label>')
            return false;
        }
        if (!isIdCardNo($("#txtIdCard").val())) {
            $("#idCardErrorPanel").html('<label for="txtIdCard" generated="true" class="error">身份证号格式错误</label>')
            return false;
        }
        //IdCardExist();
        if ($("#txtPhone").val() == "") {
            $("#phoneErrorPanel").html('<label for="txtPhone" generated="true" class="error">请输入手机号</label>')
            return false;
        }
        if (!$("#txtPhone").val().match(/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/)) {
            $("#phoneErrorPanel").html('<label for="txtPhone" generated="true" class="error">手机号格式错误</label>')
            return false;
        }
        return true;
    }

    function setDisabledTime(id, time) {
        var btn = $("#" + id);
        var originBtnValue = btn.val();
        btn.fadeIn(1000);
        btn.val(time <= 0 ? "获取验证码" : ("" + (time) + "秒内提交"));
        var hander = setInterval(function () {
            if (time <= 0) {
                clearInterval(hander);
                btn.val(originBtnValue);
                btn.removeAttr("disabled");
            } else {
                btn.attr({ "disabled": "disabled" });
                btn.val("" + (time--) + "秒内提交");
            }
        }, 1000);
    }

    function SendVCode() {
        if (Fit4GetVCode()) {
            $.post('/Interface/SengSmsVCode', { idCard: $("#txtIdCard").val(), mobile: $("#txtPhone").val(), IsAddSession: true },
                    function (d) {
                        //alert(d);                       
                        if (d == 1) {
                            setDisabledTime("btnSend", $("#hsmsInterval").val());
                            alert("验证码发送成功！");
                        }
                        else if (d == 11) {
                            alert("！");
                        }
                        else if (d == 12) {
                            alert("您接收验证码过于频繁，请稍后再试！");
                        }
                        else if (d == 7) {
                            alert("您接收验证码过于频繁，请稍后再试！");
                        }
                        else if (d == 41) {
                            $("#idCardErrorPanel").html('<label for="txtIdCard" generated="true" class="error">系统不存在此身份证号的贷款记录</label>')
                        }
                        else if (d == 42) {
                            $("#phoneErrorPanel").html('<label for="txtPhone" generated="true" class="error">系统不存在此手机号的贷款记录</label>')
                        }
                        else {
                            alert("验证码发送失败！");
                        }
                    }, 'text');
        }
    }


    //    /*表单AJAX提交封装*/
    //    function SendVCodeAjaxOnSubmit(form_id, btn_id, send_url) {
    //        $('#' + form_id).validate({
    //            errorPlacement: function (error, element) {
    //                error.appendTo(element.parent().siblings("div"));
    //            },
    //            rules: {
    //                "txtIdCard": {
    //                    required: true,
    //                    isIdCardNo: true,
    //                    IdCardExist: true
    //                },
    //                "txtPhone": {
    //                    required: true,
    //                    isMobile: true,
    //                    PhoneExist: true
    //                }
    //            },
    //            messages: {
    //                "txtIdCard": {
    //                    required: "请输入身份证号",
    //                    isIdCardNo: "身份证号格式错误",
    //                    IdCardExist: "不存在此身份证号的贷款记录"
    //                },
    //                "txtPhone": {
    //                    required: "请输入手机号",
    //                    isMobile: "手机号格式错误",
    //                    PhoneExist: "系统不存在此手机号的贷款记录"
    //                }
    //            },
    //            submitHandler: function (form) {
    //                //AJAX提交表单
    //                $(form).ajaxSubmit({
    //                    beforeSubmit: formRequest,
    //                    success: formResponse,
    //                    error: formError,
    //                    url: send_url,           //处理ajax请求的地址
    //                    type: "post",
    //                    dataType: "json",
    //                    resetForm: false,
    //                    timeout: 30000
    //                });
    //                return false;
    //            }
    //        });


    //        //表单提交前
    //        function formRequest(formData, jqForm, options) {
    ////            $("#" + btn_id).attr("disabled", "disabled");
    ////            $("#" + btn_id).val("提交中...");
    //        }

    //        //表单提交后
    //        function formResponse(d, textStatus) {
    //            if (d == 1) {
    //                setDisabledTime("btnSend", $("#hsmsInterval").val());
    //                alert("验证码发送成功！");
    //            }
    //            else if (d == 11) {
    //                alert("！");
    //            }
    //            else if (d == 12) {
    //                alert("您接收验证码过于频繁，请稍后再试！");
    //            }
    //            else if (d == 7) {
    //                alert("您接收验证码过于频繁，请稍后再试！");
    //            }
    //            else {
    //                alert("验证码发送失败！");
    //            }

    ////            $("#" + btn_id).val("确认提交");
    ////            $("#" + btn_id).removeAttr("disabled");
    //        }

    //        //表单提交出错
    //        function formError(XMLHttpRequest, textStatus, errorThrown) {
    ////            $("#" + btn_id).attr("disabled", "");
    ////            $("#" + btn_id).val("再次提交");
    //        }
    //    }

</script>
